<template>
  <div class="article-container">
    <van-nav-bar class="page-navbar"
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="content-wrap">
      <h1 class="title">{{article.title}}</h1>
      <van-cell center class="userInfo" :border="false">
        <template #title>
          <span class="name">{{article.aut_name}}</span>
        </template>
        <template #icon>
          <van-image
            class="avatar"
            round
            fit="cover"
            :src="article.aut_photo"
          />
        </template>
        <template #label>
          <div class="time">{{article.pubdate |relativeTime}}</div>
        </template>
        <van-button
          :type="article.is_followed?'default':'info'"
          :icon="article.is_followed? '':'plus'"
          class="follow"
          @click="onFollow"
          loading-type="spinner"
          :loading="isFollow"
          round
          size="small"
          >
          {{article.is_followed?'已关注':'关注'}}
        </van-button>
      </van-cell>
      <div class="markdown-body" ref="article-content" v-html="article.content"></div>
      <van-divider>正文结束</van-divider>
      <!-- 底部评论 -->
      <comment-list
        @update-count="total=$event"
        :list="commemtList"
        :source="articleId"
        @reply-click='replyClick'
        ></comment-list>
    </div>

    <!-- 底部区域 -->
    <div class="article-bottom">
      <van-button
        class="comment-btn"
        type="default"
        round
        size="small"
        @click="isPostShow=true"
      >写评论</van-button>
      <van-icon
        name="comment-o"
        :badge="total"
        color="#777"
      />
      <van-icon
        :color="article.is_collected? 'orange':'#777'"
        :name="article.is_collected? 'star':'star-o'"
        @click="onCollect"
      />
      <van-icon
        :color="article.attitude===1? 'orange':'#777'"
        :name="article.attitude===1? 'good-job':'good-job-o'"
        @click="onLike"
      />
      <van-icon name="share" color="#777777"></van-icon>
    </div>
    <!-- 评论 -->
    <van-popup v-model="isPostShow" position="bottom">
      <post-comment :target="articleId" @post-success="postSuccess"></post-comment>
    </van-popup>
    <!-- 评论回复 -->
    <van-popup v-model="isReplyShow" position="bottom">
      <comment-reply
        :comment="replyComment"
        @close="isReplyShow=false"
        v-if="isReplyShow"
        :articleId="articleId"
        ></comment-reply>
    </van-popup>
  </div>
</template>
<script>
import './github-markdown.css'
import { ImagePreview } from 'vant'
import CommentList from './components/CommentList'
import PostComment from './components/PostComment'
import CommentReply from './components/CommentReply'
import { getArticleContent, addCollect, delCollect, addLike, delLike } from '@/apis/article'
import { addFollow, delFollow } from '@/apis/user'
export default {
  name: 'Article',
  props: {
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  components: {
    CommentList,
    PostComment,
    CommentReply
  },
  data () {
    return {
      article: {},
      isFollow: false,
      isPostShow: false,
      isReplyShow: false,
      commemtList: [],
      total: 0,
      replyComment: {}
    }
  },
  created () {
    this.loadArtcleContent()
  },
  methods: {
    async loadArtcleContent () {
      const { data } = await getArticleContent(this.articleId)
      this.article = data.data
      // 1.获取文章内容的dom容器，
      // 2.得到所有的img标签，循环注册点击事件
      this.$nextTick(() => {
        const articleContent = this.$refs['article-content']
        const imgs = articleContent.querySelectorAll('img')
        const imgPath = []
        imgs.forEach((img, index) => {
          imgPath.push(img.src)
          img.onclick = function () {
            ImagePreview({
              images: imgPath,
              startPosition: index
            })
          }
        })
      })
    },
    async onFollow () {
      this.isFollow = true
      if (this.article.is_followed) {
        // 已关注
        await delFollow(this.article.aut_id)
      } else {
        // 未关注
        await addFollow(this.article.aut_id)
      }
      this.article.is_followed = !this.article.is_followed
      this.isFollow = false
    },
    async onCollect () {
      this.$toast.loading({
        message: '操作中...',
        forbidClick: true
      })
      if (this.article.is_collected) {
        // 已收藏
        await delCollect(this.articleId)

        this.$toast.success('取消收藏')
      } else {
        // 未收藏
        await addCollect(this.articleId)
        this.$toast.success('收藏成功')
      }
      this.article.is_collected = !this.article.is_collected
    },
    async onLike () {
      this.$toast.loading({
        message: '操作中...',
        forbidClick: true
      })
      if (this.article.attitude === 1) {
        // 已点赞
        await delLike(this.articleId)
        this.$toast.success('取消点赞')
        this.article.attitude = -1
      } else {
        // 未点赞
        await addLike(this.articleId)
        this.$toast.success('点赞成功')
        this.article.attitude = 1
      }
    },
    postSuccess (comment) {
      this.commemtList.unshift(comment)
      this.isPostShow = false
      this.total++
    },
    replyClick (comment) {
      this.isReplyShow = true
      this.replyComment = comment
    }
  }
}
</script>
<style lang="less" scoped>
.title{
  font-size: 20px;
  color: #3a3a3a;
  padding: 14px;
  font-weight: bold;
  background-color: #fff;
  margin: 0;
}
.userInfo{
   .avatar{
    width: 35px;
    height: 35px;
    margin-right: 8px;
  }
  .name{
    font-size: 12px;
    color: #333;
  }
  .time{
    font-size: 11px;
    color: #b4b4b4;
  }
  .follow{
    width: 85px;
    height: 29px;
  }
}
.content-wrap{
  position: fixed;
  left:0;
  top: 46px;
  right: 0;
  bottom: 44px;
  overflow-y:auto ;
  background: #fff;
  .markdown-body{
    padding: 14px;
  }
}

.article-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-sizing: border-box;
  height: 44px;
  border-top: 1px solid #d8d8d8;
  background-color: #fff;
  .comment-btn {
    width: 200px;
    height: 30px;
    border: 1px solid #eeeeee;
    font-size: 15px;
    line-height: 23px;
    color: #a7a7a7;
  }
}
</style>
